package com.hxz.config;

import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * 
 * @author hxz
 * @description tomcat配置类，增加最大连接数！
 * @data 2019年12月7日 下午2:53:26
 */
@Configuration
public class TomcatConfig {
	/**
	 * WebServer配置
	 * @return
	 */
    @Bean
    public ConfigurableServletWebServerFactory webServerFactory() {
        TomcatServletWebServerFactory tomcatFactory = new TomcatServletWebServerFactory();
        tomcatFactory.addConnectorCustomizers(new MyTomcatConnectorCustomizer());
        tomcatFactory.setPort(8080);
        tomcatFactory.setContextPath("/api-g");
        return tomcatFactory;
    }
    /**
     * 设置最大连接数
     * @author hxz
     * @description TODO
     * @data 2019年12月7日 下午3:14:32
     */
    class MyTomcatConnectorCustomizer implements TomcatConnectorCustomizer {
    	
        public void customize(Connector connector) {
            Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
            //设置最大连接数               
            protocol.setMaxConnections(20000);
            //设置最大线程数               
            protocol.setMaxThreads(2000);
            protocol.setConnectionTimeout(30000);
        }
    }

}
